package com.example.nonacademic_education.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.nonacademic_education.dao.DepartmentDao;
import com.example.nonacademic_education.data.response.DepartmentDto;
import com.example.nonacademic_education.entity.Department;
import com.example.nonacademic_education.service.DepartmentService;
import com.example.nonacademic_education.util.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:yanghua
 * @Date:2023/4/18 17:15
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentDao, Department> implements DepartmentService {
    @Resource
    private DepartmentDao departmentDao;

    @Override
    public R addOne(Department department) {
        Integer i = departmentDao.insert(department);
        Map<String,Object> data = new HashMap<>();
        data.put("department",department);
        return R.ok(data,"添加成功");
    }

    @Override
    public R deleteOne(Long id) {
        Boolean flag = departmentDao.deleteOne(id);
        return flag?R.ok("删除成功"):R.error("删除失败");
    }

    @Override
    public R updateOne(Department department) {
        departmentDao.updateById(department);
        return R.ok("修改成功");
    }

    @Override
    public R getList(int page, int size, String name) {
        if(page < 1) {
            return R.error("页数不能小于1");
        }
        page = (page-1) * size;
        List<Department> list = departmentDao.getList(page, size, name);
        Integer total = departmentDao.getNumber(name);
        Map<String,Object> data = new HashMap<>();
        data.put("list",list);
        data.put("total",total);
        return R.ok(data,"success");
    }

    @Override
    public R getIdAndNameList(String name) {
        Map<String,Object> data=new HashMap<>();
        List<DepartmentDto> idAndNameList = departmentDao.getIdAndNameList(name);
        data.put("list",idAndNameList);
        return R.ok(data,"success");
    }


}
